//
// Created by aaa on 2025/7/11.
//

#include<bits/stdc++.h>

using namespace std;
const int N = 1e6 + 10;

int n,k;
int a[N],q[N];

//单调队列
//8 3
//1 3 -1 -3 5 3 6 7
int main(){
    cin >> n >> k;
    for(int i = 1;i <= n;i++) cin >> a[i];

    int h = 1,t = 0;
    for(int i = 1;i <= n;i++){
        while(h <= t && a[q[t]] > a[i]) t--;
        q[++t] = i;
        if(q[h] < i - k + 1) h++;
        if(i >= k) cout << a[q[h]] << " ";
    }

    cout << endl;

    h = 1,t = 0;
    for(int i = 1;i <= n;i++){
        while(h <= t && a[q[t]] < a[i]) t--;
        q[++t] = i;
        if(q[h] < i - k + 1) h++;
        if(i >= k) cout << a[q[h]] << " ";
    }


    return 0;
}